package amst.system;

public class VWTPScheduler implements Scheduler {
	
	private int[] ddps = new int[3];
	
    public int findNextQueue(SystemQueue queues[], double actualTime) {
		Packet packet1 = null, packet2 = null;
		int returnIndex = 0;
		double vx = Integer.MAX_VALUE;
		double[] vis = new double[queues.length];
		for (int i = 0; i < queues.length; i++) {
			packet1 = queues[i].checkPacket();
			if(packet1 != null) {
				double vi = 0;
				System.out.print("i=" + i + " ");
				for (int j = 0; j < queues.length; j++) {
					packet2 = queues[j].checkPacket();
					if(packet2 != null) {
						double v = 0;
						System.out.print("j=" + j + ": ");
						if(i == j) {
							v = packet2.getDelay(actualTime) / ddps[j];
						} else {
							v = (packet2.getDelay(actualTime) + packet1.getServiceTime()) / ddps[j];
						}
						vis[j] = v;
						System.out.print("" + v + " ");
						vi += v;
					}
				}
				vi = vi / queues.length;
				System.out.println(" M=" + vi);
				double var = 0;
				for (int j = 0; j < queues.length; j++) {
					var += (vis[j] - vi) * (vis[j] - vi);
				}
				var = var / 3;
				if(vx > var) {
					vx = var;
					returnIndex = i;
				}
			}
		}
		 
		return returnIndex;
    }

	public void setDDP(int[] ddps) {
		this.ddps[0] = ddps[0];  
		this.ddps[1] = ddps[1];
		this.ddps[2] = ddps[2];
	}
}
